<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>My JSP 'index.jsp' starting page</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">    
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
  
  <script type="text/javascript" src="/ncrm/Public/Admin/js/jquery-1.js"></script>
   <style type="text/css"> 
    iframe{  
            border:none;  
            width:0;  
            height:0;  
        }  
    #progress{
      display:none; 
    } 
        #p_out{  
          width:300px;  
          height:12px;  
          margin:10px 0 0 0;  
          padding:1px;  
          font-size:10px;  
          border:solid #6b8e23 1px;  
        }  
        #p_in{  
          width:0%;  
          height:100%;  
          background-color:#6b8e23;  
          margin:0;  
          padding:0;  
        }  
        #dis{  
          margin:0;  
          padding:0;  
          text-align:center;  
          font-size:12px;  
          height:12px;  
          width:300px;  
        } 
        
</style>
    
  <script src="/ncrm/Public/Admin/js/b5m.js" charset="utf-8" id="b5mmain" type="text/javascript"></script><link type="text/css" href="/ncrm/Public/Admin/css/b5m-plugin.css" rel="stylesheet">
  <link type="text/css" href="/ncrm/Public/Admin/css/b5m.css" rel="stylesheet"></head>
  
  <body>
    <form action="" method="post" enctype="multipart/form-data" target="progress_iframe" name="form">  
        <input id="fileName" onclick="inputFile()" name="file" type="file">&nbsp;&nbsp;<input onclick="formSubmi()" value="提交" type="submit">  
    </form>  
    <iframe id="progress_iframe" name="progress_iframe" src="javascript:void(0)" frameborder="0"></iframe>  
  <div id="progress">
      <div id="p_out">
          <div id="p_in"></div>
      </div>  
      <div id="dis"></div>
    </div>
  

</body>
<script type="text/javascript">
     var inter = null;
     var inter1 = null;
     var a =10;
     var b= 0;
     function inputFile(){
        document.getElementById('dis').innerHTML = "新文件";
        document.getElementById('p_in').style.width = '0%';
        b=0; 
     }
    function formSubmi(){ 
       var fileName = $("#fileName").val();
       var index1=fileName.lastIndexOf(".");  
       var index2=fileName.length;
       var postf=fileName.substring(index1+1,index2);//后缀名  
       if(postf==""){
         alert("请选择要上传的文件");
         return;
       }  
       var html = $('#dis').html();

        if (html!="成功写入数据库!" && $('#progress').css('display')=='block' && html!="新文件") {
          alert('正在上传！请稍等...');
          return false;
        }
        if(html=="成功写入数据库!"){
         
          var run = confirm("该文件已上传！确认再次上传吗？");
          if(run){
             b=0;
          }else{
            return false;
          }
        }
       var fileObj = document.getElementById("fileName").files[0]; // 获取文件对象
       var FileController = "<?php echo U('Loading/Upload');?>";  //接收上传文件的后台地址 
       // FormData 对象
       var form = new FormData();
       form.append("author", "hooyes");                        // 可以增加表单数据
       form.append("file", fileObj);                           // 文件对象
       // XMLHttpRequest 对象
       var xhr = new XMLHttpRequest();
       xhr.open("post", FileController, true);
       
       if(xhr){
         xhr.addEventListener("load", uploadComplete, false);
         inter = setInterval("callback()",100);
       }
       xhr.send(form);
    } 

    function uploadComplete(evt) {
       var fileObj = document.getElementById("fileName").files[0]; 
       var size = fileObj.size;

        //读取时间
        var shijian = Math.floor(size/289497*1000);
        $("#progress").show();
        var path = evt.target.responseText;
        if(path!="false"){
             clearTimeout(inter,10);
             document.getElementById('p_in').style.width = '100%'; 
             document.getElementById('dis').innerHTML = "上传成功!";
             setTimeout("duqu()",1000);
             inter1 = setInterval("jindu()", 1000);
             setTimeout("insert()",shijian);
             uploadSuccess(path);
          
        }else{
             clearTimeout(inter,10000);
             document.getElementById('p_in').style.width = '100%';
             document.getElementById('p_in').style.backgroundColor = 'red'; 
             document.getElementById('dis').innerHTML = "上传失败!";
             return false;
        }
    }
        
     function callback(){
     $("#progress").show(); 
         document.getElementById('p_in').style.width = a+'%'; 
         document.getElementById('dis').innerHTML = "已上传："+a+"%";
         a=a+10;
         if(a==100){
           clearTimeout(inter,10);
         }
      } 

      function duqu(){
         $("#progress").show(); 
         document.getElementById('p_in').style.width = '100%'; 
         document.getElementById('dis').innerHTML = "正在读取Excel文件!";
      }
       function jindu(){
          var fileObj = document.getElementById("fileName").files[0]; 
          var size = fileObj.size;
          //读取时间
          var shijian = Math.floor(size/239497);
          var jin = Math.floor(100/shijian);
          document.getElementById('p_in').style.width = b+'%';
          b=b+jin;
       }
       
     function insert(){
        $("#progress").show();
        clearTimeout(inter1,10); 
         document.getElementById('p_in').style.width = '100%'; 
        document.getElementById('dis').innerHTML = "正在写入数据库，请稍等....!";
      }
     
      function uploadSuccess(path){
      
            $.ajax({
                    type: 'post',
                    url: "<?php echo U('Loading/insertMysql');?>",
                    data: {path:path},
                    dataType : "json", 
                    success: function(data) {
                      if(data.ok==1){
                        alert('成功写入数据库!');
                        clearTimeout(inter1,10);
                         document.getElementById('p_in').style.width = '100%'; 
                        document.getElementById('dis').innerHTML = "成功写入数据库!";
                        
                      } 
                      if(data.ok==2){
                         document.getElementById('p_in').style.backgroundColor = 'red'; 
                         document.getElementById('dis').innerHTML = "写入数据库失败!";
                         alert('写入数据库失败!');
                      }
                   }
              }); 
      }
    
  </script>

</html>